package com.yanq.liancar.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxBarTool;
import com.vondear.rxtool.view.RxToast;

import com.yanq.liancar.R;
import com.yanq.liancar.activity.ScanerCodeActivity;
import com.yanq.liancar.activity.SmallBInformationActivity;
import com.yanq.liancar.activity.SmallBOrderListActivity;
import com.yanq.liancar.activity.SmallBRescueOrderActivity;
import com.yanq.liancar.activity.SmallBSettlementActivity;
import com.yanq.liancar.activity.SmallBShopEvaluateActivity;
import com.yanq.liancar.activity.SmallBSreviceAdmActivity;
import com.yanq.liancar.activity.SmallCNewsActivity;
import com.yanq.liancar.activity.StaffAdministrationActivity;
import com.yanq.liancar.activity.TestCodeActivity;
import com.yanq.liancar.adapter.ShopServiceAdapter;
import com.yanq.liancar.base.BaseLazyFragment;
import com.yanq.liancar.bean.BQueryOrderCountBean;
import com.yanq.liancar.bean.BqueryShopAllTypeBean;
import com.yanq.liancar.bean.BqueryShopInfoBean;
import com.yanq.liancar.bean.QueryBalanceByUserBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Constant;
import com.yanq.liancar.util.LogUtils;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.app.Activity.RESULT_OK;


public class BHomeFragment extends BaseLazyFragment {
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.tv_information)
    TextView tvInformation;
    @BindView(R.id.tv_orderAdm)
    TextView tvOrderAdm;
    @BindView(R.id.tv_serviceAmd)
    TextView tvServiceAmd;
    @BindView(R.id.tv_shopevaluate)
    TextView tvShopevaluate;
    @BindView(R.id.tv_shopName)
    TextView tvShopName;
    @BindView(R.id.tv_shopAddress)
    TextView tvShopAddress;
    @BindView(R.id.setting_iv)
    ImageView settingIv;
    @BindView(R.id.rl_settlement)
    RelativeLayout rlSettlement;
    @BindView(R.id.service_gridview)
    GridView service_gridview;
    @BindView(R.id.textView6)
    TextView textView6;
    @BindView(R.id.textView7)
    TextView textView7;
    @BindView(R.id.textView8)
    TextView textView8;
    @BindView(R.id.tv_price)
    TextView tv_price;
    Unbinder unbinder;
    @BindView(R.id.ll_RescueOrder)
    LinearLayout llRescueOrder;
    @BindView(R.id.ll_CreditCard)
    LinearLayout llCreditCard;
    @BindView(R.id.ll_staff)
    LinearLayout llStaff;
    @BindView(R.id.Scan)
    ImageView Scan;
    @BindView(R.id.TestCode)
    ImageView TestCode;
    private ShopServiceAdapter serviceAdapter;
    private List<String> mList = new ArrayList<>();
    private int sid;
    private int REQUEST_CODE_SCAN = 001;

    public static BHomeFragment newInstance(String param1) {
        BHomeFragment fragment = new BHomeFragment();
        Bundle args = new Bundle();
        args.putString("agrs1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_bhome;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initOkGo();
        getJiesuan();
        serviceAdapter = new ShopServiceAdapter(getContext(), mList);
        service_gridview.setAdapter(serviceAdapter);
        LogUtils.e("111111111111","BHomeFragment======onViewCreated");
    }

    @Override
    public void onResume() {
        super.onResume();
        queryShopInfo();
        queryOrderNum();
        LogUtils.e("111111111111","BHomeFragment======onResume");
    }

    @Override
    protected void initView() {
        super.initView();
        LogUtils.e("111111111111","BHomeFragment======initView");
    }

    @Override
    protected void initData() {
        super.initData();
        LogUtils.e("111111111111","BHomeFragment======initData");
    }

    @OnClick({R.id.tv_information, R.id.tv_orderAdm, R.id.tv_serviceAmd, R.id.tv_shopevaluate, R.id.setting_iv, R.id.rl_settlement, R.id.ll_RescueOrder, R.id.ll_CreditCard, R.id.ll_staff, R.id.Scan
    ,R.id.TestCode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_information:
                if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
                    RxToast.error("请先登录");
                } else {
                    RxActivityTool.skipActivity(mActivity, SmallBInformationActivity.class);
                }

                break;
            case R.id.tv_orderAdm:
                if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
                    RxToast.error("请先登录");
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("where", "BHomeFragment");
                    RxActivityTool.skipActivity(mActivity, SmallBOrderListActivity.class, bundle);
                }
                break;
            case R.id.tv_serviceAmd:
                if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
                    RxToast.error("请先登录");
                } else {
                    RxActivityTool.skipActivity(mActivity, SmallBSreviceAdmActivity.class);
                }
                break;
            case R.id.tv_shopevaluate:
                if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
                    RxToast.error("请先登录");
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("sid", sid);
                    RxActivityTool.skipActivity(mActivity, SmallBShopEvaluateActivity.class, bundle);
                }
                break;
            case R.id.setting_iv:
                if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
                    RxToast.error("请先登录");
                } else {
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("where", "BHomeFragment");
                    RxActivityTool.skipActivity(mActivity, SmallCNewsActivity.class, bundle1);
                }
                break;
            case R.id.rl_settlement:
                if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
                    RxToast.error("请先登录");
                } else {
                    RxActivityTool.skipActivity(mActivity, SmallBSettlementActivity.class);
                }
                break;
            case R.id.ll_RescueOrder:
                if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
                    RxToast.error("请先登录");
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("shopId", sid);
                    RxActivityTool.skipActivity(mActivity, SmallBRescueOrderActivity.class, bundle);
                }
                break;
            case R.id.ll_CreditCard:
                break;
            case R.id.ll_staff:     //员工管理
                RxActivityTool.skipActivity(mActivity, StaffAdministrationActivity.class);
                break;
            case R.id.Scan:     //扫码
                Intent intent = new Intent(mActivity, ScanerCodeActivity.class);
                intent.putExtra("where", "BHomeFragment");
                intent.putExtra("sid",sid);
                startActivity(intent);
                break;
            case R.id.TestCode:     //验码
                Bundle bundle=new Bundle();
                bundle.putInt("sid",sid);
                RxActivityTool.skipActivity(mActivity, TestCodeActivity.class,bundle);
                break;
        }
    }


    //查看全部服务
    private void initOkGo() {
        if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
            RxToast.error("请先登录");
        } else {
            OkGo.<String>post(Config.s + "llc/queryShopAllType")
                    .tag(this)
                    .params("token", UserBean.getbToken(mActivity))
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            BqueryShopAllTypeBean bqueryShopAllTypeBean = gson.fromJson(response.body(), BqueryShopAllTypeBean.class);
                            if (bqueryShopAllTypeBean.isIsSuccess()) {
                                mList = bqueryShopAllTypeBean.getData();
                                serviceAdapter.setData(mList);
                            }

                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            RxToast.error("服务器连接错误");
                        }
                    });
        }
    }

    //查询商店信息
    private void queryShopInfo() {
        if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
            RxToast.error("请先登录");
        } else {
            OkGo.<String>post(Config.s + "llc/shop/queryShopInfoByBUid")
                    .tag(this)
                    .params("token", UserBean.getbToken(mActivity))
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            BqueryShopInfoBean bqueryShopInfoBean = gson.fromJson(response.body(), BqueryShopInfoBean.class);
                            if (bqueryShopInfoBean.isIsSuccess()&&bqueryShopInfoBean.getData()!=null) {
                                if (!"".equals(bqueryShopInfoBean.getData())) {
                                    tvShopName.setText(bqueryShopInfoBean.getData().getShopName());
                                    tvShopAddress.setText(bqueryShopInfoBean.getData().getShopAddress());
                                    RequestOptions options = new RequestOptions();
                                    options.placeholder(R.mipmap.car_bg);
                                    if (!bqueryShopInfoBean.getData().getShopPages().isEmpty()) {
                                        Glide.with(getContext()).load(bqueryShopInfoBean.getData().getShopPages().get(0).getWebPath()).apply(options).into(imageView);
                                    }
                                    sid = bqueryShopInfoBean.getData().getId();
                                }
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            RxToast.error("服务器连接错误");
                        }
                    });
        }
    }

    //查询商店订单数量
    private void queryOrderNum() {
        if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
            RxToast.error("请先登录");
        } else {
            OkGo.<String>post(Config.s + "llc/carServiceOrderController/queryOrderCount")
                    .tag(this)
                    .params("token", UserBean.getbToken(mActivity))
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            BQueryOrderCountBean bean = gson.fromJson(response.body(), BQueryOrderCountBean.class);
                            if (bean.isIsSuccess()) {
                                textView6.setText(bean.getData().getAll() + "");
                                textView7.setText(bean.getData().getUndo() + "");
                                textView8.setText(bean.getData().getDoX() + "");
                            } else {
                                RxToast.error(bean.getOutputMessage());
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            RxToast.error("服务器连接错误");
                        }
                    });
        }
    }

    //查询商店订单数量
    private void getJiesuan() {
        if ("".equals(UserBean.getbToken(mActivity)) || TextUtils.isEmpty(UserBean.getbToken(mActivity))) {
            RxToast.error("请先登录");
        } else {
            OkGo.<String>post(Config.s + "llc/balanceController/queryBalanceByUser")
                    .tag(this)
                    .params("token", UserBean.getbToken(mActivity))
                    .params("page", 1)
                    .params("num", 1)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            QueryBalanceByUserBean bean = gson.fromJson(response.body(), QueryBalanceByUserBean.class);
                            if (bean.isIsSuccess()) {
                                if (bean.getData() != null) {
                                    tv_price.setText("¥ " + bean.getData().getCount());
                                }
                            } else {
                                RxToast.error(bean.getOutputMessage());
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            RxToast.error("服务器连接错误");
                        }
                    });
        }
    }


}
